<template>
  <div class="drafts_page">
    <div class="left">
      <el-button
        type="success"
        size="small"
        @click="toList"
        style="background: #26a661 !important"
        icon="el-icon-back"
        >返回列表</el-button
      >
      <el-button
        type="danger"
        size="small"
        @click="delInfo"
        icon="el-icon-delete"
        :disabled="!ids.length"
        >删除</el-button
      >
    </div>
    <AddOrUpdate
      :title="title"
      :isShowAdd="isShowAdd"
      :rowFormObj="rowFormObj"
      @resetAdd="resetAdd"
      @addActivity="addActivity"
    />
    <Table
      :listLoading="listLoading"
      :listArr="listArr"
      :PlaceListArr="PlaceListArr"
      :isHasControl="true"
      @chooseDel="chooseDel"
      @delOne="delOne"
      @seeInfoFun="seeInfoFun"
    ></Table>
    <pagination
      v-show="total > 10"
      :total="total"
      :page.sync="listParams.pageNum"
      :limit.sync="listParams.pageSize"
      @pagination="getDraftsList"
    />
  </div>
</template>

<script>
import {
  draftsListApi,
  PlaceListApi,
  delActivityApi,
  createActivityApi,
} from "@/api2/politicalActivity";

import AddOrUpdate from "./addOrUpdate.vue";
import Table from "./table.vue";

export default {
  components: {
    AddOrUpdate,
    Table,
  },
  data() {
    return {
      total: 0,
      listLoading: false,
      listArr: [],
      listParams: {
        pageNum: 1,
        pageSize: 10,
        djDeptId: JSON.parse(sessionStorage.getItem("loginUserinfo")).djDeptId,
      },
      PlaceListArr: [],
      ids: [],
      title: "",
      isShowAdd: false,
      rowFormObj: {}, // 详情信息
    };
  },
  created() {
    this.getPlaceList();
    setTimeout(() => {
      this.getDraftsList();
    }, 300);
  },
  methods: {
    // 提交
    addActivity(data) {
      this.isShowAdd = true;
      let Date = {
        ...data,
        draftsStatus: 1,
      };
      createActivityApi(Date)
        .then((res) => {
          console.log(res);
          if (res.code === 0) {
            this.msgSuccess("提交成功");
            this.getDraftsList();
          } else {
            this.msgError("提交失败");
          }
          this.isShowAdd = false;
        })
        .catch((err) => {
          console.log(err);
          this.msgError("提交失败");
          this.isShowAdd = false;
        });
    },
    // 取消提交
    resetAdd() {
      this.isShowAdd = false;
    },
    // 查看详情并修改
    seeInfoFun(row) {
      console.log(row);
      this.rowFormObj = row;
      this.title = "详情";
      this.isShowAdd = true;
    },
    // 草稿箱:删除某一个
    delOne(id) {
      delActivityApi(id)
        .then((res) => {
          console.log(res);
          if (res.code === 0) {
            this.msgSuccess("删除成功");
            this.getDraftsList();
          } else {
            this.msgError("删除失败");
          }
        })
        .catch((err) => {
          console.log(err);
          this.msgError("删除失败");
        });
    },

    // 批量删除
    delInfo() {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        let ids = this.ids.join(",");
        delActivityApi(ids)
          .then((res) => {
            console.log(res);
            if (res.code === 0) {
              this.msgSuccess("删除成功");
              this.getDraftsList();
            } else {
              this.msgError("删除失败");
            }
          })
          .catch((err) => {
            console.log(err);
            this.msgError("删除失败");
          });
      });
    },

    // 删除选中
    chooseDel(ids) {
      this.ids = ids;
    },
    // 获取活动地点
    getPlaceList() {
      PlaceListApi()
        .then((res) => {
          console.log(res);
          this.PlaceListArr = res.data;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 获取草稿箱数据
    getDraftsList() {
      this.listLoading = true;
      draftsListApi(this.listParams)
        .then((res) => {
          console.log(res);
          if (res.code == 0) {
            this.total = res.total;
            this.listArr = res.data;
          } else {
            this.listArr = [];
          }
          this.listLoading = false;
        })
        .catch((err) => {
          console.log(err);
          this.listArr = [];
          this.listLoading = false;
        });
    },

    // 切换为数据列表
    toList() {
      this.$emit("change", "List");
    },
  },
};
</script>

<style>
.drafts_page {
  padding: 20px;
}
</style>
